/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.ModelState;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.ui.rich.full.command.LastZoomCommand;

/**
 * A class representing creation dialog on screen.<br>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since July 2004
 */
public final class ScalesSettingsDialog extends SimpleDialog {

	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
    private JPanel scalesPane = new JPanel();
	
	private DecimalField loadScaleField;
    private DecimalField supportScaleField;
    private DecimalField nodeScaleField;
    private DecimalField deformScaleField;
    private DecimalField diagramScaleField;
    private DecimalField displaceScaleField;
    private DecimalField concForcePointScaleField;
    private DecimalField preStrainsScaleField;
    private DecimalField iniEqForceScaleField;
	private DecimalField reactionsScaleField;
	
	private NumberFormat numberFormat;
    private ModelState state;
	
//*****************************************************************************
	
    /**
     * Constructor method, calling super with the especific title.
     *
     * @param parent The parent of the class.
     * @see gui.InsaneInternalInterface
     */
    public ScalesSettingsDialog(InsaneInternalInterface parent, ModelState s) {
        this.parent = parent;
        state = s;
		
        setUpFormat();
		
        //Sets fields according to model
        loadScaleField  = new DecimalField(state.loadScale, 10, numberFormat);
        supportScaleField = new DecimalField(state.supportScale, 10, numberFormat);
        nodeScaleField = new DecimalField(state.nodeScale, 10, numberFormat);
        deformScaleField = new DecimalField(state.deformScale, 10, numberFormat);
        diagramScaleField = new DecimalField(state.diagramScale, 10, numberFormat);
        displaceScaleField = new DecimalField(state.displaceScale, 10, numberFormat);
        concForcePointScaleField = new DecimalField(state.concForcePointScale, 10, numberFormat);
        preStrainsScaleField = new DecimalField(state.preStrainsScale, 10, numberFormat);
        iniEqForceScaleField = new DecimalField(state.iniEqForceScale, 10, numberFormat);
		reactionsScaleField = new DecimalField(state.reactionsScale, 10, numberFormat);
		
//		 Sets up the scales panel
        JPanel scsPane = new JPanel();
        scsPane.setLayout(new BoxLayout(scsPane, BoxLayout.Y_AXIS));
        scsPane.add(scalesPane);
        scalesPane.setLayout(new GridLayout(10,2,2,2));
        scalesPane.setMaximumSize(new Dimension(500,300));
        scalesPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dScaDialog01")),
        BorderFactory.createEmptyBorder(2,2,2,2)));
        scalesPane.add(new JLabel(parent.getResourceBundle().getString("dScaDialog02")));
        scalesPane.add(loadScaleField);
        scalesPane.add(new JLabel(parent.getResourceBundle().getString("dScaDialog03")));
        scalesPane.add(supportScaleField);
        scalesPane.add(new JLabel(parent.getResourceBundle().getString("dScaDialog04")));
        scalesPane.add(nodeScaleField);
        scalesPane.add(new JLabel(parent.getResourceBundle().getString("dScaDialog05")));
        scalesPane.add(deformScaleField);
        scalesPane.add(new JLabel(parent.getResourceBundle().getString("dScaDialog06")));
        scalesPane.add(diagramScaleField);
		scalesPane.add(new JLabel(parent.getResourceBundle().getString("dScaDialog07")));
        scalesPane.add(displaceScaleField);
		scalesPane.add(new JLabel(parent.getResourceBundle().getString("dScaDialog08")));
        scalesPane.add(concForcePointScaleField);
		scalesPane.add(new JLabel(parent.getResourceBundle().getString("dScaDialog09")));
        scalesPane.add(preStrainsScaleField);
		scalesPane.add(new JLabel(parent.getResourceBundle().getString("dScaDialog10")));
        scalesPane.add(iniEqForceScaleField);
		scalesPane.add(new JLabel(parent.getResourceBundle().getString("dScaDialog11")));
        scalesPane.add(reactionsScaleField);
		
        setPane(scalesPane);
		
    }
	
//*****************************************************************************
    
    public int showDialog() {
    	return super.showDialog(parent, parent.getResourceBundle().getString("dScaDialog12"));
    }
	
//*****************************************************************************
	
    /**
     * This method is executed on OK button click.
     *
     */
    public void approvedPerform() {
        try {
            state.loadScale = loadScaleField.getValue();
            state.supportScale = supportScaleField.getValue();
            state.nodeScale = nodeScaleField.getValue();
            state.deformScale = deformScaleField.getValue();
            state.diagramScale = diagramScaleField.getValue();
            state.displaceScale = displaceScaleField.getValue();
            state.concForcePointScale = concForcePointScaleField.getValue();
            state.preStrainsScale = preStrainsScaleField.getValue();
            state.iniEqForceScale = iniEqForceScaleField.getValue();
			state.reactionsScale = reactionsScaleField.getValue();
			
			parent.setModel(parent.getModel());
            this.setVisible(false);
			
			LastZoomCommand cmd = new LastZoomCommand(parent);
			cmd.execute();
			
        } catch (Exception err) {
            Toolkit.getDefaultToolkit().beep();
            this.setVisible(false);
        }
    }
    
//*****************************************************************************
	
    /**
     * Sets the number formater
     */
    private void setUpFormat() {
        numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
        numberFormat.setMaximumFractionDigits(3);
    }
	
//*****************************************************************************
}
